library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 9.2
summary(factor(df$Q5A))
summary(factor(df$Q6A))
summary(factor(df$Q11))
df2 <- df

# Important
df2$Q5A[df2$Q5A>3] <- NA
df2$Q5A[df2$Q5A==3] <- 0
df2$Q5A[df2$Q5A==2] <- 0.5
df2$Q5A[df2$Q5A==1] <- 1

df2$Q5B[df2$Q5B>3] <- NA
df2$Q5B[df2$Q5B==3] <- 0
df2$Q5B[df2$Q5B==2] <- 0.5
df2$Q5B[df2$Q5B==1] <- 1

df2$Q5C[df2$Q5C>3] <- NA
df2$Q5C[df2$Q5C==3] <- 0
df2$Q5C[df2$Q5C==2] <- 0.5
df2$Q5C[df2$Q5C==1] <- 1

df2$Q5D[df2$Q5D>3] <- NA
df2$Q5D[df2$Q5D==3] <- 0
df2$Q5D[df2$Q5D==2] <- 0.5
df2$Q5D[df2$Q5D==1] <- 1

df2$Q5E[df2$Q5E>3] <- NA
df2$Q5E[df2$Q5E==3] <- 0
df2$Q5E[df2$Q5E==2] <- 0.5
df2$Q5E[df2$Q5E==1] <- 1

df2$Q5F[df2$Q5F>3] <- NA
df2$Q5F[df2$Q5F==3] <- 0
df2$Q5F[df2$Q5F==2] <- 0.5
df2$Q5F[df2$Q5F==1] <- 1

df2$Q5G[df2$Q5G>3] <- NA
df2$Q5G[df2$Q5G==3] <- 0
df2$Q5G[df2$Q5G==2] <- 0.5
df2$Q5G[df2$Q5G==1] <- 1

df2$Q5H[df2$Q5H>3] <- NA
df2$Q5H[df2$Q5H==3] <- 0
df2$Q5H[df2$Q5H==2] <- 0.5
df2$Q5H[df2$Q5H==1] <- 1

colnames(df2) # 25:32

df2$REAL <- rowSums(df2[,c(25,32)],na.rm=FALSE)/2 # Q5A+Q5H
df2$HUMAN <- rowSums(df2[,c(28,30)],na.rm=FALSE)/2 # Q5D+Q5F
df2$SECURITY <- rowSums(df2[,c(29,31)],na.rm=FALSE)/2 # Q5E+Q5G
df2$DOMESTIC <- rowSums(df2[,c(26,27)],na.rm=FALSE)/2 # Q5B+Q5C

df2 <- df2[df2$Q11 < 5 & df2$P_ASSIGN1==1,]

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

f.real <- svyby(~ REAL, ~Q11, w2_design, svymean, na.rm = TRUE)
f.hum <- svyby(~ HUMAN, ~Q11, w2_design, svymean, na.rm = TRUE)
f.sec <- svyby(~ SECURITY, ~Q11, w2_design, svymean, na.rm = TRUE)
f.dom <- svyby(~ DOMESTIC, ~Q11, w2_design, svymean, na.rm = TRUE)

# Realpolitik
g.real <- ggplot(f.real, aes(x=factor(Q11),y=REAL,label=round(REAL,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Realpolitik Missions\n")

# Humanitarian
g.hum <- ggplot(f.hum, aes(x=factor(Q11),y=HUMAN,label=round(HUMAN,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Humanitarian Missions\n")

# Homeland
g.sec <- ggplot(f.sec, aes(x=factor(Q11),y=SECURITY,label=round(SECURITY,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Homeland Security\n")

# Domestic
g.dom <- ggplot(f.dom, aes(x=factor(Q11),y=DOMESTIC,label=round(DOMESTIC,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Domestic Social Work\n")

grid.arrange(g.real,g.hum,g.sec,g.dom,ncol=2,nrow=2)
